package nix.web.action;

import java.io.File;
import java.util.List;

import nix.domain.Customer;
import nix.service.CustomerService;
import nix.utils.PageBean;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class CustomerAction extends ActionSupport implements ModelDriven<Customer> {

	private CustomerService customerService;

	// 对象模型接收参数
	private Customer customer = new Customer();
	private Integer currentPage;
	private Integer pageSize;
	
	// 上传文件自动封装至File属性
	private File cust_file;
	// 自动封装文件名至xxxFileName属性
	private String cust_fileFileName;
	// 自动封装文件MEMO类型至xxxContentType
	private String cust_fileContentType;

	public String list() throws Exception {

		DetachedCriteria dc = DetachedCriteria.forClass(Customer.class);
		if (StringUtils.isNoneBlank(this.customer.getCust_name())) {
			dc.add(Restrictions.like("cust_name", "%" + this.customer.getCust_name() + "%"));
		}
		PageBean pb = customerService.getPageBean(dc, currentPage, pageSize);
		ActionContext.getContext().put("pageBean", pb);
		return "list";
	}
	
	public String add() throws Exception {

		if (cust_file != null) {
			cust_file.renameTo(new File("D:/upload/" + cust_fileFileName));
		}
		customerService.save(this.customer);
		return "toList";
	}
	
	public String toEdit() throws Exception {

		Customer c = customerService.getById(customer.getCust_id());
		ActionContext.getContext().put("customer", c);
		return "edit";
	}
	
	public String industryCount() throws Exception {

		List<Object[]> list = customerService.getIndustryCount();
		ActionContext.getContext().put("list", list);
		return "industryCount";
	}

	@Override
	public Customer getModel() {
		return this.customer;
	}
	
	public void setCustomerService(CustomerService customerService) {
		this.customerService = customerService;
	}
	
	public Integer getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(Integer currentPage) {
		this.currentPage = currentPage;
	}

	public Integer getPageSize() {
		return pageSize;
	}

	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}

	public File getCust_file() {
		return cust_file;
	}

	public void setCust_file(File cust_file) {
		this.cust_file = cust_file;
	}

	public String getCust_fileFileName() {
		return cust_fileFileName;
	}

	public void setCust_fileFileName(String cust_fileFileName) {
		this.cust_fileFileName = cust_fileFileName;
	}

	public String getCust_fileContentType() {
		return cust_fileContentType;
	}

	public void setCust_fileContentType(String cust_fileContentType) {
		this.cust_fileContentType = cust_fileContentType;
	}
}
